home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-20 | 8.2 KB | 311 lines | [TEXT/PJMM] |
- unit MathDeclarations;
-
- interface
-
- uses
- {$IFC undefined THINK_PASCAL}
- Types, Memory,
- {$endc}
- Assertions;
- const
-
- { MAX_LENGTH = 1000;}
- { MAX_LENGTH = 32000;}
- MAX_LENGTH = 64000;
- { MAX_LENGTH = 178956970;}
- { MAX_LENGTH = MAXLONGINT;}
- ERR_RESULT = -999;
-
- global_epsilon = 1E-7;
-
- type
- { Je définis ces types une fois pour toutes afin d'éviter les problèmes multi-plateformes }
- TReal = double;
- TInteger = LongInt;
- Str10 = string[10];
- TString = Str10;
- TMethodePermutation = (none, vector, matrix, double, triple);
-
- { Nouvelle définition universelle de matrices }
- TStrVector = array[1..MAX_LENGTH] of TString;
- TStrVectorPtr = ^TStrVector;
- TBoolVector = array[1..MAX_LENGTH] of Boolean;
- TBoolVectorPtr = ^TBoolVector;
- TIntegerVector = array[1..MAX_LENGTH] of TInteger;
- TIntegerVectorPtr = ^TIntegerVector;
- TRealVector = array[1..MAX_LENGTH] of TReal;
- TRealVectorPtr = ^TRealVector;
-
- TColumnHeader = record
- n: TInteger;
- data: TRealVectorPtr;
- end;
-
- TMatrixColumn = array[1..MAX_LENGTH] of TColumnHeader;
- TMatrixColumnPtr = ^TMatrixColumn;
-
- TMatrix = record
- n, p, nbObjets, root: TInteger; { Paramètres de la matrice }
- col: TMatrixColumnPtr; { Accès aux données }
- colName, rowName: TStrVectorPtr; { Noms des colonnes }
- indirection, { Vecteur d'indirection pour permutations }
- group: TIntegerVectorPtr; { Pour grouper des objets ou des variables? }
- permMeth: TMethodePermutation; { Méthode de permutation si nécessaire }
- s1, s2, s3: StringPtr; { Pour conserver de l'information quelconque }
- end;
- TMatrixPtr = ^TMatrix;
-
- { Création et accès aux matrices }
- function InitMatrix (var m: TMatrixPtr): Boolean;
- function TrouveNombreObjets (x: TInteger): TInteger;
- function NewMatrix (var m: TMatrixPtr;
- n, p: TInteger;
- wantRowNames, wantColNames: Boolean): boolean;
- procedure DisposeMatrix (var m: TMatrixPtr);
-
- { Création d'un vecteur d'indirection pour permutations }
- procedure CreerVecteurIndirection (mat: TMatrixPtr);
- procedure CreerVecteurGroupes (mat: TMatrixPtr);
-
- function GetElement (m: TMatrixPtr;
- i, j: TInteger): TReal;
- procedure SetElement (var m: TMatrixPtr;
- i, j: TInteger;
- value: TReal);
- function GetColName (m: TMatrixPtr;
- j: TInteger): TString;
- procedure SetColName (var m: TMatrixPtr;
- j: TInteger;
- s: TString);
- function GetRowName (m: TMatrixPtr;
- i: TInteger): TString;
- procedure SetRowName (var m: TMatrixPtr;
- i: TInteger;
- s: TString);
- function CreateTString (var theStrVectorPtr: TStrVectorPtr;
- count: TInteger): Boolean;
- implementation
-
- { Mettre les valeurs de la matrice à zéro }
- function InitMatrix (var m: TMatrixPtr): Boolean;
- begin
- m := TMatrixPtr(NewPtrClear(SizeOf(TMatrix)));
- Assert(m <> nil);
- if m <> nil then begin { Paranoid }
- m^.n := 0;
- m^.p := 0;
- m^.nbObjets := 0;
- m^.root := 0;
- m^.col := nil;
- m^.colName := nil;
- m^.rowName := nil;
- m^.indirection := nil;
- m^.group := nil;
- m^.permMeth := none;
- m^.s1 := nil;
- m^.s2 := nil;
- m^.s3 := nil;
- InitMatrix := true;
- end
- else
- InitMatrix := false;
- end; { InitMatrix }
-
- function TrouveNombreObjets (x: TInteger): TInteger;
- var
- sqrtInteger: TInteger;
- begin
- {$IFC not undefined THINK_PASCAL}
- sqrtInteger := trunc(Sqrt(8 * x + 1));
- {$elsec}
- sqrtInteger := system.trunc(Sqrt(8 * x + 1));
- {$endc}
- if (sqrtInteger * sqrtInteger) = (8 * x + 1) then { la racine carrée est un nombre entier }
- TrouveNombreObjets := (1 + sqrtInteger) div 2
- else
- TrouveNombreObjets := 0;
- end; { TrouveNombreObjets }
-
- function NewMatrix (var m: TMatrixPtr;
- n, p: TInteger;
- wantRowNames, wantColNames: Boolean): boolean;
- var
- result: boolean;
- i: TInteger;
- begin
- result := false; { Ca ne marche pas pour le moment }
- { As-t-on déjà une matrice? Possible... }
- if (m <> nil) then
- if (m^.n = n) and (m^.p = p) then
- result := true;
-
- if not result then { On n'avait pas déjà de matrice... }
- if InitMatrix(m) and (n > 0) and (p > 0) then begin
- result := true;
- m^.n := n;
- m^.p := p;
- m^.nbObjets := TrouveNombreObjets(n);
- if wantColNames then
- result := CreateTString(m^.colName, p);
- if wantRowNames then
- result := CreateTString(m^.rowName, n);
- m^.col := TMatrixColumnPtr(NewPtrClear(SizeOf(TColumnHeader) * p));
- if m^.col <> nil then begin
- for i := 1 to p do begin { Créer chaque colonne }
- m^.col^[i].n := n;
- m^.col^[i].data := TRealVectorPtr(NewPtrClear(SizeOf(TReal) * n));
- end; { for i }
- for i := 1 to p do { Vérifier que tout s'est bien passé }
- if (m^.col^[i].data = nil) then
- result := false;
- end { if m^.col }
- else
- result := false;
- end; { if > 0 }
- NewMatrix := result;
- end; { NewMatrix }
-
- procedure DisposeMatrix (var m: TMatrixPtr);
- var
- i: TInteger;
- begin
- if m <> nil then begin
- if m^.col <> nil then begin
- for i := 1 to m^.p do begin { Effacer chaque colonne }
- m^.col^[i].n := 0;
- if m^.col^[i].data <> nil then
- DisposePtr(Ptr(m^.col^[i].data));
- end; { for i }
- DisposePtr(Ptr(m^.col));
- end; { if m.col }
- if (m^.colName <> nil) then
- DisposePtr(Ptr(m^.colName));
- if (m^.rowName <> nil) then
- DisposePtr(Ptr(m^.rowName));
- if (m^.indirection <> nil) then
- DisposePtr(Ptr(m^.indirection));
- if (m^.group <> nil) then
- DisposePtr(Ptr(m^.group));
- if (m^.s1 <> nil) then
- DisposePtr(Ptr(m^.s1));
- if (m^.s2 <> nil) then
- DisposePtr(Ptr(m^.s2));
- if (m^.s3 <> nil) then
- DisposePtr(Ptr(m^.s3));
- DisposePtr(Ptr(m));
- end;
- m := nil;
- end; { DisposeMatrix }
-
- procedure CreerVecteurIndirection (mat: TMatrixPtr);
- var
- i, n: TInteger;
- begin
- Assert(mat <> nil);
- if mat^.permMeth = vector then
- n := mat^.n { permutations sur toute la longueur d'une colonne }
- else
- n := mat^.nbObjets; { Permutations simultanées lignes/colonnes }
- mat^.indirection := TIntegerVectorPtr(NewPtrClear(n * SizeOf(TInteger)));
- if mat^.indirection <> nil then
- for i := 1 to n do
- mat^.indirection^[i] := i;
- end; { CreerVecteurIndirection }
-
- procedure CreerVecteurGroupes (mat: TMatrixPtr);
- var
- i, n: TInteger;
- begin
- Assert(mat <> nil);
- if mat^.permMeth = vector then
- n := mat^.n { groupes d'objets dans matrice rectangulaire }
- else
- n := mat^.nbObjets; { matrice de distance }
- mat^.group := TIntegerVectorPtr(NewPtrClear(n * SizeOf(TInteger)));
- if mat^.group <> nil then
- for i := 1 to n do
- mat^.group^[i] := 0; { 0 = aucun groupe pour le moment }
- end; { CreerVecteurGroupes }
-
- function GetElement;
- var
- result: TReal;
- begin
- result := ERR_RESULT;
- Assert(m <> nil);
- if m <> nil then
- if (i > 0) and (i <= m^.n) and (j > 0) and (j <= m^.p) then
- if m^.col <> nil then
- if m^.col^[j].data <> nil then
- result := m^.col^[j].data^[i];
- GetElement := result;
- end; { GetElement }
-
- procedure SetElement;
- begin
- Assert(m <> nil);
- if m <> nil then
- if (i > 0) and (i <= m^.n) and (j > 0) and (j <= m^.p) then
- if m^.col <> nil then
- if m^.col^[j].data <> nil then
- m^.col^[j].data^[i] := value;
- end; { SetElement }
-
- function GetColName;
- var
- result: TString;
- begin
- result := 'none';
- Assert(m <> nil);
- if m <> nil then
- if (j > 0) and (j <= m^.p) then
- if m^.colName <> nil then
- result := m^.colName^[j]
- else
- result := StringOf(j);
- GetColName := result;
- end; { GetColName }
-
- procedure SetColName;
- begin
- Assert(m <> nil);
- if m <> nil then
- if (j > 0) and (j <= m^.p) then
- if m^.colName <> nil then
- m^.colName^[j] := s;
- end; { SetColName }
-
-
- function GetRowName;
- var
- result: TString;
- begin
- result := 'none';
- Assert(m <> nil);
- if m <> nil then
- if (i > 0) and (i <= m^.n) then
- if m^.rowName <> nil then
- result := m^.rowName^[i]
- else
- result := StringOf(i);
- GetRowName := result;
- end; { GetRowName }
-
- procedure SetRowName;
- begin
- Assert(m <> nil);
- if m <> nil then
- if (i > 0) and (i <= m^.n) then
- if m^.rowName <> nil then
- m^.rowName^[i] := s;
- end; { SetRowName }
-
- function CreateTString;
- begin
- CreateTString := True;
- theStrVectorPtr := TStrVectorPtr(NewPtrClear(SizeOf(TString) * count));
- if (theStrVectorPtr = nil) then
- CreateTString := False;
- end; { CreateTString }
-
- end. { unit MathDeclarations }